package com.ballgame;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;

import static com.ballgame.GameUtil01.*;
public class MyGameFrame extends Frame {
    //图片加载一次就可以
    Image bgImg = GameUtil01.getImage("images01/bg.jpg");
    Image planeImg = GameUtil01.getImage("images01/plane.png");

    Plane plane = new Plane(planeImg,250,250,7);
    //首先定义数组，再初始化，最后画出来
    Shell[] shells = new Shell[50];//定义数组，现在数组是空的，要初始化
    Explode explode;//声明炮弹类，new只是飞机碰撞爆炸的一瞬间，在这里没有用

    //定义两变量，计算游戏时间
    Date startTime = new Date();
    Date endTime;
    int period;//玩了多小秒

    //初始化窗口
    public void launchFrame() {
        this.setTitle("飞机大战--尚学堂.百战程序员");
        this.setVisible(true);//窗口默认不可见，需要让他可见
        this.setSize(600, 600);//窗口尺寸
        this.setLocation(250, 100);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);//关闭窗口
            }
        });
        //内部类，内部方法可以调用
        new PaintThread().start(); //启动窗口绘制线程
        //启动键盘监听
        this.addKeyListener(new keyMonitor());
        //初始化50发炮弹
        for(int i = 0;i<shells.length;i++){

                shells[i] = new Shell();

        }
    }

    //重写paint方法
    @Override
    public void paint(Graphics g) {
        g.drawImage(bgImg,0,0,FRAME_WIDTH,FRAME_HIGHT,null);
        plane.drawMySelf(g);//飞机自己画自己
        //画炮弹
        for(int i=0;i<shells.length;i++) {
            if(shells[i]!=null) {
                shells[i].drawMySelf(g);
                //炮弹碰到飞机后爆炸
                boolean peng = shells[i].getRec().intersects(plane.getRec());
                if(peng){

                    plane.live = false;
                    endTime = new Date();//飞机爆炸时间
                    period = (int)((endTime.getTime()-startTime.getTime())/1000);//endTime是毫秒
                    if(explode == null){
                        explode = new Explode(plane.x,plane.y);//将爆炸的方位引入，用构造器
                    }
                    explode.draw(g);


                }

            }

        }

        if(!plane.live){
            printInfo(g,"游戏时间："+period+"秒",40,200,300,Color.WHITE);
        }


       /* g.drawImage(bgImg,0,0,FRAME_WIDTH,FRAME_HIGHT,null);
        g.drawImage(planeImg,x,y,PLANE_WIDTH,PLANE_HIGHT,null);
        x--; //x-=3 :x=x-3,飞机移动速加快
        y--;*/
      /*  //定义旧的颜色，用来画完后将颜色变回原来的
        Color old = g.getColor();
       // g.setColor(Color.blue);  画笔为蓝色
        g.setColor(new Color(0,0,255));
        //g 就是一支画笔
        g.drawLine(100,50,400,400);
        g.drawRect(100,50,400,400);//矩形
        g.drawOval(100,50,400,400);//圆
        g.drawString("SxT",100,100);
        g.setColor(old);//变回原来的颜色*/
    }

    //显示时间
    //画出来，内容，字体大小，位置，颜色
    public void printInfo(Graphics g,String str,int size,int x,int y,Color color){
        Font oldFont = g.getFont();
        Color oldColor = g.getColor();//定义未有画之前的属性，
        Font f = new Font("宋体",Font.BOLD,size);
        g.setFont(f);
        g.setColor(color);
        g.drawString(str, x, y);

        //变回原来的颜色
        g.setColor(oldColor);
        g.setFont(oldFont);

    }


    //键盘监听内部类，因为要调用飞机类，所以用内部类，方便使用外部类的内容
    class keyMonitor extends KeyAdapter{
        @Override
        public void keyPressed(KeyEvent e) {
            plane.addDirection(e);

        }

        @Override
        public void keyReleased(KeyEvent e) {

            plane.minusDirection(e);
        }
    }
  //重画线程  内部类
    class PaintThread extends Thread {
        @Override
        public void run() {
            while (true){
                repaint(); //系统内部去调用paint(Graphics g)，不是我们去调用
                try {
                    Thread.sleep(40);//1s=1000ms,
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        MyGameFrame frame = new MyGameFrame();
        frame.launchFrame();
    }

    //处理绶冲问题，闪屏
    private Image offScreenImage =null;

    public  void update(Graphics g){
        if(offScreenImage == null)
            offScreenImage = this.createImage(FRAME_WIDTH,FRAME_HIGHT);//这是游戏窗口

        Graphics g0ff = offScreenImage.getGraphics();
        paint(g0ff);
        g.drawImage(offScreenImage,0,0,null);
    }
}